﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevSky.WebParts.DBConnectWP;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using System.Web.UI.WebControls.WebParts;

namespace DevSky.WebParts.DBConnectWP
{
    public class Configuration
    {
        private DevSkyDBConnectWP dbwp = null;
        private string openerURL = string.Empty;
        private Guid  currentWPGuid = Guid.Empty;
        #region Properties
        public string DBConnectString { get; private set; }
        public string DBObjectName { get; private set; }
        public string DBObjectType { get; private set; }
        public bool ShowDeleteMenuEntry { get; set; }
        public bool ShowEditLinkMenuEntry { get; set; }
        public bool ShowReadOnlyMenuEntry { get; set; }
        public bool ShowNewMenuEntry { get; set; }
        public bool ShowCustomLinks { get; set; }
        public string DialogTitle { get; set; }
        public List<CustomDataColumn> CustomDataColumns { get; set; }
        #endregion

        public Configuration(Guid webPartGuid, string openerURL)
        {
            this.openerURL = openerURL;
            this.currentWPGuid = webPartGuid;            
        }

        public bool ReadConfigFromPage()
        {
            if (CurrentWebPart != null)
            {
                DBConnectString = dbwp.ConnectString;
                DBObjectType = CurrentWebPart.DataSourceType;
                DBObjectName = CurrentWebPart.DataSource;
                ShowCustomLinks = dbwp.ShowCustomLinks;
                ShowDeleteMenuEntry = dbwp.ShowDeleteLink;
                ShowEditLinkMenuEntry = dbwp.ShowEditLink;
                ShowNewMenuEntry = dbwp.ShowNewLink;
                ShowReadOnlyMenuEntry = dbwp.ShowReadOnlyLink;
                CustomDataColumns = CurrentWebPart.CustomDataColumns;
                DialogTitle = CurrentWebPart.DetailDialogTitle;
                return true;
            }
            return false;
        }

        public void SaveConfiguration()
        {
            using (SPLimitedWebPartManager wpm = SPContext.Current.Web.GetLimitedWebPartManager(openerURL, PersonalizationScope.Shared))
            {
                foreach (System.Web.UI.WebControls.WebParts.WebPart webpart in wpm.WebParts)
                {
                    if (webpart is DevSkyDBConnectWP) // TODO compare wp guid
                    {
                        dbwp = (webpart as DevSkyDBConnectWP);
                        dbwp.CustomDataColumns = CustomDataColumns;
                        dbwp.ShowCustomLinks = ShowCustomLinks;
                        dbwp.ShowDeleteLink = ShowDeleteMenuEntry;
                        dbwp.ShowEditLink = ShowEditLinkMenuEntry;
                        dbwp.ShowNewLink = ShowNewMenuEntry;
                        dbwp.ShowReadOnlyLink = ShowReadOnlyMenuEntry;
                        dbwp.DetailDialogTitle = DialogTitle;
                        wpm.SaveChanges(dbwp);
                        break;
                    }
                }
            }
        }

        private DevSkyDBConnectWP CurrentWebPart
        {
            get
            {
                if (dbwp == null)
                {
                    using (SPLimitedWebPartManager wpm = SPContext.Current.Web.GetLimitedWebPartManager(openerURL, PersonalizationScope.Shared))
                    {
                        foreach (System.Web.UI.WebControls.WebParts.WebPart webpart in wpm.WebParts)
                        {
                            if (webpart is DevSkyDBConnectWP) // TODO: compare webpart guid
                            {
                                dbwp = (webpart as DevSkyDBConnectWP);
                                break;
                            }
                        }
                    }
                }
                return dbwp;
            }
        }

    }
}
